<?php
namespace PcHome\Controller;
use Common\Controller\PcHomeBaseController;
class ArticleController extends PcHomeBaseController {

    /*
     * 文章页
     * */
    public function index(){
        $article_id = I('get.article_id', 0, 'intval');
        $cate_id    = I('get.cate_id', 0, 'intval');
        $article_model = M("CmsArticle");

        $article = $article_model
            ->alias("a")
            ->field('a.*,b.cate_name')
            ->join("__ADMIN__ b ON a.post_author = b.admin_id")
            ->where(array('a.id'=>$article_id,'b.cate_id'=>$cate_id))
            ->find();

        if (empty($article)) {
            $this->error('文章不存在或已经被删除!');
        }

        //获取栏目信息
        $cate = x_get_cate_info($cate_id);

        //自行根据网站需要考虑，是否需要判断
        $can_do = x_check_user_action('article' . $article_id, 0 , false, 60);
        if($can_do){
            //更新点击数
            $article_model->where(array('article_id'=>$article_id))->setInc('click');
            $article['click']+=1;
        }

        //上一篇/下一篇文章
        $next = $article_model->where(
            array(
                "create_time" => array("egt",$article['create_time']),
                "article_id"  => array('neq',$article_id),
                "status"      => 1,
                'is_delete'   => 0,
                'cate_id'     => $cate_id))
            ->order("create_time asc")->find();
        $prev = $article_model->where(
            array(
                "create_time"=>array("elt",$article['create_time']),
                "article_id"=>array('neq',$article_id), "status"=>1,
                'is_delete'=>0,
                'cate_id'=>$cate_id))
            ->order("create_time desc")->find();

        $this->assign("next",$next);
        $this->assign("prev",$prev);

        //文章分页处理
        $content_data = x_content_page($article['content']);
        $article['content'] = $content_data['content'];

        $addition = json_decode($article['addition'], true);

        $this->assign("page",$content_data['page']);
        $this->assign($article);
        $this->assign("addition", $addition);
        $this->assign("cate", $cate);
        $this->assign("article_id",$article_id);

        //模板
        $tplname = $cate["article_tpl"];
        $tplname = empty($addition['template']) ? $tplname : $addition['template'];

        $this->display(":$tplname");
    }

    /*
     * 点赞
     * **/
    public function dianzan() {
        $this->check_login();

        $article_id    = I('article_id', 0, 'intval');
        $article_model = M("CmsArticle");

        //检测是否赞过
        $can_like = x_check_user_action('article' . $article_id, 1);

        if ($can_like) {
            $article_model->save(array("article_id"=>$article_id,"zan"=>array("exp","zan+1")));
            $this->success('赞好啦！', 1, 1);
        } else {
            $this->error('您已赞过啦！', 0, 0);
        }
    }
}